package br.com.werp.regras.geral;

import br.com.werp.regras.geral.util.Constantes;
import br.com.werp.regras.geral.util.FormatDate;
import br.com.werp.regras.geral.util.Util;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import java.sql.Timestamp;

import java.util.ResourceBundle;


/**
 * @author Sobreira
 * Criado em 11/08/2004
 */
public class GerenciadorErros {
    public static final ResourceBundle bundle =
        ResourceBundle.getBundle(Constantes.CONFIG);
    public static final boolean modoDebug =
        "S".equals(bundle.getString(Constantes.MODODEBUG)) ? true : false;

    public static ErroPadrao logarErro(Object e) {
        ErroPadrao erroPadrao = null;

        if (e == null) {
            erroPadrao = new ErroPadrao("Erro nulo no GerenciadorErros.");
            e = erroPadrao;
        } else if (!(e instanceof Exception)) {
            erroPadrao = new ErroPadrao("Erro desconhecido.");
        }

        if (e instanceof Exception && !(e instanceof ErroPadrao)) {
            erroPadrao = new ErroPadrao((Exception) e);
        } else {
            erroPadrao = (ErroPadrao) e;
        }

        gravarLogErro(erroPadrao);

        if (modoDebug) {
            saidaPadrao(erroPadrao.getMessage());
            erroPadrao.printStackTrace();
        }

        return erroPadrao;
    }

    public synchronized static void gravarLogErro(ErroPadrao erro) {
        try {
            String catalina =
                System.getProperties().getProperty("catalina.home");

            if (Util.isNullOrEmpty(catalina)) {
                return;
            }

            String dia =
                FormatDate.format(new Timestamp(System.currentTimeMillis()),
                    Constantes.DATA_FORMATO_YYYYMMDD);
            String fileName =
                catalina + File.separatorChar + "logs" + File.separatorChar +
                "LogsSistema" + dia + ".log";
            FileWriter fileWriter = new FileWriter(new File(fileName), true);
            PrintWriter printWriter = new PrintWriter(fileWriter);
            printWriter.println("------------------------------------------");
            printWriter.println("Numero do erro : " + erro.getNumErro());
            printWriter.println("Mensagem : " + erro.getMessage());
            printWriter.println("Classe : " + erro.getClasseErroCriada());
            erro.printStackTraceResumido(printWriter);
            printWriter.println("------------------------------------------");
            printWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static void escreverLogErro(String linha) {
        try {
            String catalina =
                System.getProperties().getProperty("catalina.home");

            if (Util.isNullOrEmpty(catalina)) {
                return;
            }

            String dia =
                FormatDate.format(new Timestamp(System.currentTimeMillis()),
                    Constantes.DATA_FORMATO_YYYYMMDD);
            String fileName =
                catalina + File.separatorChar + "LogsSistema" + dia + ".log";
            FileWriter fileWriter = new FileWriter(new File(fileName), true);
            PrintWriter printWriter = new PrintWriter(fileWriter);
            printWriter.println(linha);
            printWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void logarErro(String s) {
        saidaPadrao(s);
    }

    public static void logarErro(Exception e, String s) {
        if (modoDebug) {
            e.printStackTrace();
        }

        saidaPadrao(e.getMessage());
        saidaPadrao(s);
    }

    protected static void saidaPadrao(String s) {
        if (modoDebug) {
            System.out.println(s + " <= " +
                FormatDate.format(new Timestamp(System.currentTimeMillis()),
                    Constantes.DATA_FORMATO_COMPLETO));
        }
    }

    public static void main(String[] args) throws Exception {
    }
}
